package com.chai.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.chai.constant.MessageConstant;
import com.chai.service.MemberService;
import com.chai.service.SetmealService;
import com.chai.vo.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/Report")
public class ReportController {

    @Reference
    MemberService memberService;

    @Reference
    SetmealService setmealService;

    @GetMapping("/getMemberReport")
    public Result getMemberReport(){
        try {
            Map map = new HashMap<>();

            //1.获取最近12个月份字符在
            List<String> months = new ArrayList<String>();
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH,-12); //向前偏移12个月    2020-11
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            for(int i = 0; i<12; i++) {
                calendar.add(Calendar.MONTH,1);// 向后偏移1个月   2020-12    2021-11
                months.add(sdf.format(calendar.getTime()));
            }

            //2.获取每一个月注册用户数
            List<Integer> memberCount = memberService.findMemberCountByMonths(months);

            //3.封装数据
            map.put("months",months);
            map.put("memberCount",memberCount);

            //4.返回数据
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @GetMapping("/getSetmealReport")
    public Result getSetmealReport(){
        try {
            Map map = new HashMap();

            //获取套餐名称
            List<String> setmealNames = new ArrayList<>();


            //获取预约套餐的数量，由图表自动生成占比情况
            List<Map<String, Object>> setmealCount = setmealService.getSetmealReport();

            for (Map<String, Object> mapData : setmealCount) {
                String  name = (String)mapData.get("name");
                setmealNames.add(name);

            }

            map.put("setmealNames",setmealNames);
            map.put("setmealCount",setmealCount);


            return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);

        }
    }
}
